package com.myapp.ruyixing.http;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.myapp.ruyixing.model.DiscountDetail;
import com.myapp.ruyixing.model.DiscountDetail.DetailItem;

public class DiscountDetailParser extends RYJSONParser {
	@Override
	public Object parse(JSONObject jsonObject) {

		DiscountDetail detail = new DiscountDetail();
		detail.list = new ArrayList<DiscountDetail.DetailItem>();

		ModelParser.parseModel(detail, jsonObject);

		JSONObject data = jsonObject.optJSONObject("data");

		JSONArray list = data.optJSONArray("list");

		for (int i = 0; list != null && i < list.length(); i++) {
			JSONObject object = list.optJSONObject(i);
			if (object != null) {

				DetailItem item = new DetailItem();
				item.content = object.optString("content", "");
				item.title = object.optString("title", "");
				item.type = object.optString("type", "");
				item.address = object.optString("address", "");
				item.score = object.optInt("score", 0);
				item.discountid = object.optString("discountid", "");
				item.endtime = object.optString("endtime", "");
				item.favorite = object.optInt("favorite", 0);
				item.originalprice = object.optString("originalprice", "");
				item.presentprice = object.optString("presentprice", "");
				item.shopid = object.optString("shopid", "");
				item.shopname = object.optString("shopname", "");
				item.signup = object.optString("signup", "");
				item.tele = object.optString("tele", "");

				item.pics = new ArrayList<String>();
				JSONArray array = object.optJSONArray("pics");
				if (array != null && array.length() > 0) {
					for (int j = 0; j < array.length(); j++) {
						String picurl = array.optString(j, "");
						item.pics.add(picurl);
					}
				}
				item.pic4app = new ArrayList<String>();
				JSONArray array1 = object.optJSONArray("pic4app");
				if (array1 != null && array1.length() > 0) {
					for (int j = 0; j < array1.length(); j++) {
						String picurl = array1.optString(j, "");
						item.pic4app.add(picurl);
					}
				}
				detail.list.add(item);
			}
		}

		return detail;

	}
}
